/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.expression.dcExpression;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public class Label extends GUIModule {
    private dcExpression labelExp;
    private VarMatcher refreshTrigger;
    private JLabel label;
    
    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        component = label = new JLabel();
        super.setupProperties();
    }

    @Override
    public void setColor(Color value) {
        super.setColor(value);
        if (label!=null) {
            if (!negative)
                label.setForeground(color);
            else label.setBackground(color);
        }
    }

    @Override
    public void setNegative(Boolean value) {
        super.setNegative(value);
        if (label==null) return;
        
        if (!negative) {
            label.setBackground(Color.BLACK);
            label.setForeground(color);
        } else {
            label.setForeground(Color.BLACK);
            label.setBackground(color);
            label.setOpaque(true);
        }
    }

    @Override
    public void setFont(Font value) {
        super.setFont(value);
        if (label!=null) label.setFont(font);
        label.revalidate();
    }

    public void setLabel(dcExpression value) {
        labelExp = value;
        if (label!=null) refreshLabel();
    } public dcExpression getLabel() { return labelExp; }

    public void setRefreshTrigger(VarMatcher value) {
        refreshTrigger = value;
    } public VarMatcher getRefreshTrigger() { return refreshTrigger; }

    @Override
    public void start() {
        super.start();
        refreshLabel();
    }
    
    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (refreshTrigger.matches(in, channel, this)) {
            setInVars(in, timestamp, channel);
            refreshLabel();
        }
    }

    private void refreshLabel() {
        try {
            label.setText((String)labelExp.evaluate(this));
        } catch (IllegalArgumentException ie) {
            log(ie.getMessage(), MSG.WARNING);
        }
    }        
}
